using Domain;
using Domain.Services;
using Domain.Entities;
using Domain.Entities.Enums;
using Domain.Permissions;
using System;
using System.Web.UI.WebControls;
using UserInterface.Misc;
using System.Configuration;

namespace UserInterface
{
    public partial class EditMessage : BoardPageBase
    {
        private Message message;

        protected void Page_Load(object sender, EventArgs e)
        {
            int messageId = Convert.ToInt32(Request.QueryString["messageId"]);
            string pageTitle = (string)GetLocalResourceObject("Title");

            message = MessageService.GetById(messageId);

            ForumPermissionManager forumPermissionManager = PermissionManager.CreateForumPermissionManager(message.Topic.Forum);
            if (CurrentSession.IsGuest
                || (message.Topic.Locked && !forumPermissionManager.Moderate)
                || (
                    !forumPermissionManager.Moderate
                    && (
                        !forumPermissionManager.EditOwn
                        || message.Poster.Id != CurrentSession.UserId
                    )
                )
            )
            {
                DieMessage(DieMessageType.InsufficientPermissions, "InsufficientPermissions");
            }

            MasterPage.TitleGenerator.PageTitle = pageTitle;
            MasterPage.HeaderText = pageTitle;
            MasterPage.AddPathItem(message.Topic.Forum.Name, "ViewForum.aspx?forumId=" + message.Topic.Forum.Id);
            MasterPage.AddPathItem(message.Topic.Subject, "ViewTopic.aspx?topicId=" + message.Topic.Id);
            MasterPage.AddPathItem(pageTitle, "EditMessage.aspx?messageId=" + message.Id);

            PreviewMessage.Visible = false;

            if (!Page.IsPostBack)
            {
                Attachments.InitialAttachments = message.Attachments;

                Editor.Text = message.Text;

                ModeratorOptionsPlaceHolder.Visible = forumPermissionManager.Moderate;
                IsForModeratorOnlyCheckBox.Checked = message.ModeratorsOnly;
            }
        }

        protected void EditButton_Click(object sender, EventArgs e)
        {
            string text = Editor.Text.Trim();

            if (text == String.Empty)
            {
                MasterPage.Messages.AddCritical(Resources.Resource.InvalidFormData);
            }
            else
            {
                bool isForModeratorsOnly = IsForModeratorOnlyCheckBox.Checked;
                MessageService.UpdateMessage(message, text, isForModeratorsOnly);

                string filePathFormat =
                    ConfigurationManager.AppSettings["AttachmentsDirectory"]
                    + "/"
                    + ConfigurationManager.AppSettings["AttachmentFileNameFormat"];
                MessageService.EditAttachments(
                    message,
                    Attachments.AttachmentsWithFiles,
                    filePathFormat
                );
                
                Response.Redirect("ViewTopic.aspx?topicId=" + message.Topic.Id + "&messageId=" + message.Id + "#message_" + message.Id);
            }
        }

        protected void PreviewButton_Click(object sender, EventArgs e)
        {
            //TODO
            //Message previewMessageEntity = new Message();
            //previewMessageEntity.Text = Editor.Text;
            //previewMessageEntity.Poster = message.Poster;
            //previewMessageEntity.PostDateTime = message.PostDateTime;
            //previewMessageEntity.Attachments = Attachments.AttachmentsWithFiles;
            //PreviewMessage.Visible = true;
            //PreviewMessage.Message = previewMessageEntity;
        }

        protected void CancelButton_Click(object sender, EventArgs e)
        {
            Response.Redirect("ViewTopic.aspx?topicId=" + message.Topic.Id + "&messageId=" + message.Id + "#message_" + message.Id);
        }
    }
}
